package hy;

import hy.beans.User;
import hy.dao.IUserDao;
import hy.uitils.Util;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.lang.reflect.Method;
import java.util.List;

public class App {
    SqlSession session;;
    @Before
    public void init(){
        session = Util.getSession();
    }
    @After
    public void close(){
        // 增删改执行之后必须提交
        session.commit();
        session.close();
    }
    @Test
    public void selectOne(){
        User o = session.selectOne("abc.selectOnUser", 1);
        System.out.println(o);
    }
    @Test
    public void selectAll(){
        List list = session.selectList("abc.selectAllUser");
        System.out.println(list);
    }
    private User initUser(){
        User u = new User();
        u.setName("tom22");
        u.setPass("abc22");
        return u;
    }

    @Test
    public void insert(){
        User u = initUser();
        int rs = session.insert("abc.insertUserWithKey",u);
        System.out.println(u);
    }
    @Test
    public void delete(){
        int rs = session.delete("abc.deleteUserById",4);
        System.out.println(rs);
    }
    @Test
    public void update(){
        User u = initUser();
        u.setUserId(3);
        int rs = session.update("abc.updateUserById",u);
        System.out.println(rs);
    }

    @Test
    public void mapper(){
        IUserDao dao = session.getMapper(IUserDao.class);

//    反射获取其接口及方法
        Class c = dao.getClass();
        System.out.println(c.getSimpleName());
        Class<?>[] interfaces = c.getInterfaces();
        System.out.println(interfaces[0].getSimpleName());
        Method[] m = c.getDeclaredMethods();
        for (Method method : m) {
            System.out.println(method.getName());
        }

        System.out.println(dao.selectById(1));
//    返回影响的行数
//        dao.insertUser(initUser());
//        dao.deleteUserById(9);
    }

    @Test
    public void select1(){
        IUserDao dao = session.getMapper(hy.dao.IUserDao.class);
        System.out.println(dao.select1("admin1"));
    }
    @Test
    public void select2(){
        IUserDao dao = session.getMapper(hy.dao.IUserDao.class);
        System.out.println(dao.select2("t_user","name"));
    }
    @Test
    public void select3(){
        IUserDao dao = session.getMapper(hy.dao.IUserDao.class);
        System.out.println(dao.select3());
    }
    @Test
    public void select5(){
        IUserDao dao = session.getMapper(hy.dao.IUserDao.class);
        System.out.println(dao.select5("tom22","abc22"));
    }

    @Test
    public void select6(){
        IUserDao dao = session.getMapper(hy.dao.IUserDao.class);
        System.out.println(dao.selectAll_tel());
    }    @Test
    public void select7(){
        IUserDao dao = session.getMapper(hy.dao.IUserDao.class);
        System.out.println(dao.selectAll_phone());
    }

}
